<?php
declare(strict_types=1);

namespace App\Controller\v1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Service\UserService;
use App\Service\OfficeService;
use App\Service\ExcelService;
use App\Middleware\Api\AuthMiddleware;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Response;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class OfficeController extends AbstractController
{
    #[Inject]
    protected UserService $userServ;
    #[Inject]
    protected OfficeService $officeServ;
    #[Inject]
    protected ExcelService $excelServ;

    #创建科室
    #[RequestMapping(path: "create", methods: "post")]
    public function create()
    {
        $hospital_id = $this->request->input("hospital_id", 0);
        $office_type_id = $this->request->input("office_type_id", 0);
        $name = $this->request->input("name", "");
        $user_name = $this->request->input("user_name", "");
        if(empty($hospital_id)){
            return $this->fmt_response(ReturnCode::para_miss,'医院错误');
        }
        if(empty($office_type_id)){
            return $this->fmt_response(ReturnCode::para_miss,'所属科室错误');
        }
        if(empty($name)){
            return $this->fmt_response(ReturnCode::para_miss,'科室名称错误');
        }
        if(mb_strlen($name)>30){
            return $this->fmt_response(ReturnCode::para_miss,'科室名称过长');
        }
        if(empty($user_name)){
            return $this->fmt_response(ReturnCode::para_miss,'姓名错误');
        }
        if(mb_strlen($user_name)>30){
            return $this->fmt_response(ReturnCode::para_miss,'姓名过长');
        }
        $user_id = $this->request->getAttribute('user_id');
        $check_exists = $this->officeServ->checkExists($user_id);
        if($check_exists){
            return $this->fmt_response(ReturnCode::para_miss,'您已加入其它科室');
        }
        $params = [
            'hospital_id'=>$hospital_id,
            'office_type_id'=>$office_type_id,
            'name'=>$name,
            'user_name'=>$user_name,
            'user_id'=>$user_id
        ];
        $res = $this->officeServ->createOffice($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #修改科室
    #[RequestMapping(path: "update", methods: "post")]
    public function update()
    {
        $office_id = $this->request->input("office_id", 0);
        $hospital_id = $this->request->input("hospital_id", 0);
        $office_type_id = $this->request->input("office_type_id", 0);
        $name = $this->request->input("name", "");
        $user_name = $this->request->input("user_name", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室ID错误');
        }
        if(!empty($name) && mb_strlen($name)>30){
            return $this->fmt_response(ReturnCode::para_miss,'科室名称过长');
        }
        if(!empty($user_name) && mb_strlen($user_name)>30){
            return $this->fmt_response(ReturnCode::para_miss,'姓名过长');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'hospital_id'=>$hospital_id,
            'office_type_id'=>$office_type_id,
            'name'=>$name,
            'user_name'=>$user_name
        ];
        $res = $this->officeServ->updateOffice($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #加入科室
    #[RequestMapping(path: "join", methods: "post")]
    public function join()
    {
        $office_id = $this->request->input("office_id", 0);
        $user_name = $this->request->input("user_name", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        if(empty($user_name)){
            return $this->fmt_response(ReturnCode::para_miss,'姓名错误');
        }
        if(mb_strlen($user_name)>30){
            return $this->fmt_response(ReturnCode::para_miss,'姓名过长');
        }
        $user_id = $this->request->getAttribute('user_id');
        $check_exists = $this->officeServ->checkExists($user_id);
        if($check_exists){
            return $this->fmt_response(ReturnCode::para_miss,'您已有科室，不可多次加入');
        }
        $params = [
            'office_id'=>$office_id,
            'user_id'=>$user_id,
            'role'=>"member",
            'user_name'=>$user_name
        ];
        $res = $this->officeServ->addMember($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false, '失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #科室基本信息
    #[RequestMapping(path: "base", methods: "post")]
    public function base()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $res = $this->officeServ->baseInfo();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #科室成员信息-列表
    #[RequestMapping(path: "members", methods: "post")]
    public function members()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $res = $this->officeServ->membersInfo($user_id);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #科室成员排序
    #[RequestMapping(path: "member-sort", methods: "post")]
    public function memberSort()
    {
        $office_id = $this->request->input("office_id", 0);
        $member_id = $this->request->input("member_id", 0);
        $sort = $this->request->input("sort", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($member_id)){
            return $this->fmt_response(ReturnCode::para_miss,'成员ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $res = $this->officeServ->saveMemberSort($member_id,$sort);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #科室成员信息-详情
    #[RequestMapping(path: "member-detail", methods: "post")]
    public function memberDetail()
    {
        $office_id = $this->request->input("office_id", 0);
        $member_id = $this->request->input("member_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($member_id)){
            return $this->fmt_response(ReturnCode::para_miss,'成员ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $res = $this->officeServ->memberDetail($member_id);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #科室成员信息-保存
    #[RequestMapping(path: "member-save", methods: "post")]
    public function memberSave()
    {
        $office_id = $this->request->input("office_id", 0);
        $member_id = $this->request->input("member_id", 0);
        $avatar = $this->request->input("avatar", "");
        $realname = $this->request->input("realname", "");
        $phone = $this->request->input("phone", "");
        $positional = $this->request->input("positional", 0);
        $level = $this->request->input("level", 0);
        $education = $this->request->input("education", 0);
        $ycapital = $this->request->input("ycapital", "");
        $workno = $this->request->input("workno", "");
        $workbed = $this->request->input("workbed", "");
        $remark = $this->request->input("remark", "");
        $sort = $this->request->input("sort", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($member_id)){
            return $this->fmt_response(ReturnCode::para_miss,'成员ID错误');
        }
        if(!empty($phone)){
            if(!Verify::phoneFormat($phone)){
                return $this->fmt_response(ReturnCode::para_miss,'手机号格式错误');
            }
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap && $user_id!=$member_id){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'member_id'=>$member_id,
            'avatar'=>$avatar,
            'realname'=>$realname,
            'phone'=>$phone,
            'positional'=>$positional,
            'level'=>$level,
            'education'=>$education,
            'ycapital'=>$ycapital,
            'workno'=>$workno,
            'workbed'=>$workbed,
            'remark'=>$remark,
            'sort'=>$sort
        ];
        $res = $this->officeServ->memberSave($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #解散科室
    #[RequestMapping(path: "del", methods: "post")]
    public function del()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $res = $this->officeServ->delOffice();
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #删除成员
    #[RequestMapping(path: "del-member", methods: "post")]
    public function delMember()
    {
        $office_id = $this->request->input("office_id", 0);
        $member_id = $this->request->input("member_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($member_id)){
            return $this->fmt_response(ReturnCode::para_miss,'成员ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $res = $this->officeServ->delMember($member_id);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #获取科室班次
    #[RequestMapping(path: "trad", methods: "post")]
    public function trad()
    {
        $office_id = $this->request->input("office_id", 0);
        $status = $this->request->input("status", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        if($status==1){
            $res = $this->officeServ->getTrads();
        }else{
            $res = $this->officeServ->getTradsAll();
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #保存排班-保存并发布
    #[RequestMapping(path: "save-calendar", methods: "post")]
    public function saveCalendar()
    {
        $office_id = $this->request->input("office_id", 0);
        $data = $this->request->input("data", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($data)){
            return $this->fmt_response(ReturnCode::para_miss,'数据错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'data'=>$data
        ];
        $channel = new \Swoole\Coroutine\Channel();
        $officeServ = $this->officeServ;
        co(function () use ($channel,$officeServ,$params) {
            $this->officeServ->saveCalendar($params);
        });
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #保存排班-仅保存
    #[RequestMapping(path: "save-caltemp", methods: "post")]
    public function saveCaltemp()
    {
        $office_id = $this->request->input("office_id", 0);
        $data = $this->request->input("data", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($data)){
            return $this->fmt_response(ReturnCode::para_miss,'数据错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'data'=>$data
        ];
        $res = $this->officeServ->saveCalendarTemp($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false, '失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #开始排班前检测
    #[RequestMapping(path: "before-trad", methods: "post")]
    public function beforeTrad()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $res = $this->officeServ->getNextTrad();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #排班信息
    #[RequestMapping(path: "get-calendar", methods: "post")]
    public function getCalendar()
    {
        $office_id = $this->request->input("office_id", 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        $is_public = $this->request->input("is_public", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_member = $this->officeServ->checkIsMember($user_id);
        if(!$check_is_member){
            return $this->fmt_response(ReturnCode::para_miss,'你还不是此科室成员');
        }
        $params = [
            'office_id'=>$office_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date,
        ];
        if(empty($is_public)){
            $res = $this->officeServ->getCalendarTemp($params);
        }else{
            $res = $this->officeServ->getCalendar($params);
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #排班备注
    #[RequestMapping(path: "calendar-remark", methods: "post")]
    public function calendarRemark()
    {
        $office_id = $this->request->input("office_id", 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($begin_date)){
            return $this->fmt_response(ReturnCode::para_miss,'开始时间错误');
        }
        if(empty($end_date)){
            return $this->fmt_response(ReturnCode::para_miss,'结束时间错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $params = [
            'office_id'=>$office_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date
        ];
        $res = $this->officeServ->getCalendarRemark($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #保存排班备注
    #[RequestMapping(path: "save-calendar-remark", methods: "post")]
    public function saveCalendarRemark()
    {
        $office_id = $this->request->input("office_id", 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        $content = $this->request->input("content", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($begin_date)){
            return $this->fmt_response(ReturnCode::para_miss,'开始时间错误');
        }
        if(empty($end_date)){
            return $this->fmt_response(ReturnCode::para_miss,'结束时间错误');
        }
        if(empty($content)){
            return $this->fmt_response(ReturnCode::para_miss,'内容不能为空');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date,
            'content'=>$content
        ];
        $res = $this->officeServ->saveCalendarRemark($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #退出科室
    #[RequestMapping(path: "quit", methods: "post")]
    public function quit()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if($check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者不能退出科室，只能解散科室');
        }
        $params = [
            'user_id'=>$user_id
        ];
        $res = $this->officeServ->quitOffice($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #获取排班设置
    #[RequestMapping(path: "get-set", methods: "post")]
    public function getSet()
    {
        $office_id = $this->request->input("office_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $params = [
            'office_id'=>$office_id
        ];
        $res = $this->officeServ->getOfficeSet($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #保存排班设置
    #[RequestMapping(path: "save-set", methods: "post")]
    public function saveSet()
    {
        $office_id = $this->request->input("office_id", 0);
        $tmod = $this->request->input("tmod", 0);
        $tunit = $this->request->input("tunit", 0);
        $tbed = $this->request->input("tbed", 0);
        $work_cycle = $this->request->input("work_cycle", 0);
        $work_day = $this->request->input("work_day", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $params = [
            'office_id'=>$office_id,
            'tmod'=>$tmod,
            'tunit'=>$tunit,
            'tbed'=>$tbed,
            'work_cycle'=>$work_cycle,
            'work_day'=>$work_day
        ];
        $res = $this->officeServ->saveOfficeSet($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #获取班种详细信息
    #[RequestMapping(path: "trad-detail", methods: "post")]
    public function getTrad()
    {
        $office_id = $this->request->input("office_id", 0);
        $trad_id = $this->request->input("trad_id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($trad_id)){
            return $this->fmt_response(ReturnCode::para_miss,'详情ID错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $params = [
            'office_id'=>$office_id,
            'trad_id'=>$trad_id
        ];
        $res = $this->officeServ->getOfficeTradDetail($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #保存排班设置
    #[RequestMapping(path: "save-trad", methods: "post")]
    public function saveTrad()
    {
        $office_id = $this->request->input("office_id", 0);
        $id = $this->request->input("id", 0);
        $type = $this->request->input("type", "");
        $name = $this->request->input("name", "");
        $color = $this->request->input("color", 0);
        $work_time = $this->request->input("work_time", "");
        $work_hours = $this->request->input("work_hours", 0);
        $coe = $this->request->input("coe", 0);
        $sort = $this->request->input("sort", 0);
        $status = $this->request->input("status", -1);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(mb_strlen($name)>4){
            return $this->fmt_response(ReturnCode::para_miss,'名称过长');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'id'=>$id,
            'type'=>$type,
            'name'=>$name,
            'color'=>$color,
            'work_time'=>$work_time,
            'work_hours'=>$work_hours,
            'coe'=>$coe,
            'sort'=>$sort,
            'status'=>$status
        ];
        $res = $this->officeServ->saveOfficeTrad($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #删除班次
    #[RequestMapping(path: "del-trad", methods: "post")]
    public function delTrad()
    {
        $office_id = $this->request->input("office_id", 0);
        $id = $this->request->input("id", 0);
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'id'=>$id
        ];
        $res = $this->officeServ->delOfficeTrad($params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #批量排序
    #[RequestMapping(path: "batch-sort", methods: "post")]
    public function batchSort()
    {
        $office_id = $this->request->input("office_id", 0);
        $sort_for = $this->request->input("sort_for", "");
        $data = $this->request->input("data", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        if(empty($sort_for)){
            return $this->fmt_response(ReturnCode::para_miss,'排序应用对象错误');
        }
        if(empty($data)){
            return $this->fmt_response(ReturnCode::para_miss,'数据错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        switch ($sort_for) {
            case 'member':
                $res = $this->officeServ->memberBatchSort($data);
                break;
            case 'trad':
                $res = $this->officeServ->tradBatchSort($data);
                break;
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #排班校验-空校验
    #[RequestMapping(path: "check-empty", methods: "post")]
    public function checkEmpty()
    {
        $office_id = $this->request->input("office_id", 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $check_is_cap = $this->officeServ->checkIsCap($user_id);
        if(!$check_is_cap){
            return $this->fmt_response(ReturnCode::para_miss,'创建者才能做此操作');
        }
        $params = [
            'office_id'=>$office_id,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date,
        ];
        $data = $this->officeServ->getCalendarTemp($params);
        $params['data'] = $data;
        $res = $this->officeServ->checkEmpty($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false, '失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #导出排班表
    #[RequestMapping(path: "export-trad", methods: "post")]
    public function exportTrad()
    {
        $office_id = $this->request->input("office_id", 0);
        $begin_date = $this->request->input("begin_date", "");
        $end_date = $this->request->input("end_date", "");
        if(empty($office_id)){
            return $this->fmt_response(ReturnCode::para_miss,'科室错误');
        }
        $user_id = $this->request->getAttribute('user_id');
        $this->officeServ->setOfficeId($office_id);
        $office_exists = $this->officeServ->checkExistsOffice();
        if(!$office_exists){
            return $this->fmt_response(ReturnCode::para_miss,'科室不存在或已解散');
        }
        $office_info = $this->officeServ->baseInfo();
        $trad_remark = $this->officeServ->tradRemark($begin_date,$end_date);
        $trad_info = $this->officeServ->getTrads();
        $params = [
            'office_id'=>$office_id,
            'office_info'=>$office_info,
            'trad_remark'=>$trad_remark,
            'trad_info'=>$trad_info,
            'begin_date'=>$begin_date,
            'end_date'=>$end_date
        ];
        $export_data = $this->officeServ->getCalendar($params);
        $res = $this->excelServ->doExport($export_data['list'],$params);
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $res);
    }
    #下载排班表文件
    #[RequestMapping(path: "export-file", methods: "post")]
    public function exportFile()
    {
        $file_name = $this->request->input("file_name", "");
        if(empty($file_name)){
            return $this->fmt_response(ReturnCode::para_miss,'文件错误');
        }
        $check_res = $this->excelServ->checkFile($file_name);
        if(!$check_res){
            return $this->fmt_response(ReturnCode::para_miss,'文件不存在');
        }
        $file_data = $this->excelServ->getDataSource($file_name);
        $response = new Response();
        return $response->withHeader('content-description', 'File Transfer')
        ->withHeader('content-type', 'application/octet-stream')
        ->withHeader('content-disposition', "attachment; filename={$file_name}")
        ->withHeader('content-transfer-encoding', 'binary')
        ->withHeader('pragma', 'public')
        ->withBody(new SwooleStream($file_data));
    }
}